package stream;

import reference.Student;

import java.util.Arrays;
import java.util.List;

/**
 * @author LiuYan
 * @date 2021/11/19 8:32
 * 自然排序：Comparable 类内部自定义的排序（实现Comparable接口，重写CompareTo的方法定义排序规则）
 * 定制排序：Comparator 自定义排序规则（Collections.sort(new Comparator(){重写compare方法})或者Arrays.sort(new Comparator(){重写compare方法})）
 */
public class Part08 {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("aaa", "ccc", "bbb", "ddd", "bbb");
        list.stream().sorted().forEach(System.out::println);

        List<Student> studentList = Arrays.asList(new Student(10, "zhangsan"),
                new Student(20, "lisi"),
                new Student(30, "wangwu"),
                new Student(40, "goudan"));
        studentList.stream().sorted((e1, e2) -> {
            if (e1.getAge().compareTo(e2.getAge()) == 0) {
                return e1.getName().compareTo(e2.getName());
            } else {
                return e1.getAge().compareTo(e2.getAge());
            }

        }).forEach(System.out::println);

    }


}
